package setting

import (
	"gin-demo/conf"
	"github.com/go-ini/ini"
	"log"
	"time"
)

// setting全局配置 声明变量
var (
	Cfg *ini.File

	RunMode      string
	HttpPort     int
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	ApiHost      string

	PageSize  int
	JwtSecret string

	// chatgpt3配置
	ChatGPT3Conf conf.ChatGPT3Config
	// chatgpt3.5配置
	ChatGPT35Conf conf.ChatGPT3Config
	// deepseek配置
	DeepSeekConf conf.ChatGPT3Config
)

// 初始化配置
func SetUp() {
	var err error
	Cfg, err = ini.Load("conf/app.ini")
	if err != nil {
		// 发生了错误，打印日志并退出程序
		log.Fatalf("Fail to parse 'conf/app.ini: %v'", err)
	}

	LoadBase()
	LoadServer()
	LoadApp()
	LoadChatGPT()
}

func LoadBase() {
	// Section("") 应该是获取ini 没有模块的配置信息
	// MustString 给默认值
	RunMode = Cfg.Section("").Key("RUN_MODE").MustString("debug")
	ApiHost = Cfg.Section("").Key("API_HOST").MustString("http://localhost:8899")
}

func LoadServer() {
	server, err := Cfg.GetSection("server")
	if err != nil {
		log.Fatalf("Fail to get section 'server' : %v", err)
	}

	HttpPort = server.Key("HTTP_PORT").MustInt(8000)
	ReadTimeout = time.Duration(server.Key("READ_TIMEOUT").MustInt(60)) * time.Second
	WriteTimeout = time.Duration(server.Key("WRITE_TIMEOUT").MustInt(60)) * time.Second
}

func LoadApp() {
	sec, err := Cfg.GetSection("app")
	if err != nil {
		log.Fatalf("Fail to get section 'app': %v", err)
	}

	JwtSecret = sec.Key("JWT_SECRET").MustString("!@)*#)!@U#@*!@!)")
	PageSize = sec.Key("PAGE_SIZE").MustInt(10)
}

func LoadChatGPT() {
	// 读取配置
	chatgpt3, err2 := Cfg.GetSection("chatgpt3")
	if err2 != nil {
		log.Fatalf("Fail to get section 'server' : %v", err2)
	}
	endPoint := chatgpt3.Key("END_POINT").MustString("")
	apiKey := chatgpt3.Key("API_KEY").MustString("")
	proxy := chatgpt3.Key("PROXY").MustString("")

	ChatGPT3Conf = conf.ChatGPT3Config{
		END_POINT: endPoint,
		API_KEY:   apiKey,
		PROXY:     proxy,
	}

	// chatgpt3.5的配置
	chatgpt35, err2 := Cfg.GetSection("chatgpt3.5")
	if err2 != nil {
		log.Fatalf("Fail to get section 'server' : %v", err2)
	}
	endPointNew := chatgpt35.Key("END_POINT").MustString("")
	apiKeyNew := chatgpt35.Key("API_KEY").MustString("")
	proxyNew := chatgpt35.Key("PROXY").MustString("")

	ChatGPT35Conf = conf.ChatGPT3Config{
		END_POINT: endPointNew,
		API_KEY:   apiKeyNew,
		PROXY:     proxyNew,
	}

	// deepseek的配置
	deepseekConf, err2 := Cfg.GetSection("deepseek")
	if err2 != nil {
		log.Fatalf("Fail to get section 'server' : %v", err2)
	}
	deepSeekEndPointNew := deepseekConf.Key("END_POINT").MustString("")
	deepSeekApiKeyNewDK := deepseekConf.Key("API_KEY").MustString("")
	deepSeekProxyNewDK := deepseekConf.Key("PROXY").MustString("")

	DeepSeekConf = conf.ChatGPT3Config{
		END_POINT: deepSeekEndPointNew,
		API_KEY:   deepSeekApiKeyNewDK,
		PROXY:     deepSeekProxyNewDK,
	}

	// log.Println(ChatGPT3Conf)
	// log.Println(ChatGPTNewConf)
}
